<template>
  <div class="app-container" id="printArea">
    <el-row class="do-not-print">
      <!--    搜索区    -->
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="daterangeCreateDate"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="统计单位" prop="deptId">
          <treeselect v-model="queryParams.deptId" :options="deptOptions" placeholder="请选择归属部门"
                      style="width: 200px;"/>
        </el-form-item>
        <el-form-item label="交易状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="交易状态" clearable>
            <el-option
              v-for="dict in dict.type.trade_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" icon="el-icon-printer" size="mini" v-print="'#printArea'">打 印</el-button>
          <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-row style="margin-top: 20px;margin-bottom: 20px;">
      <el-card>
        <div slot="header">
          <el-row :gutter="10">
            <el-col :span="6">
              <i class="el-icon-receiving"></i>
              <span>统计结果</span>
            </el-col>
            <el-col :span="6"></el-col>
            <el-col :span="6">
                <span v-if="daterangeCreateDate.length>1">
                  {{ daterangeCreateDate[0] + '至' + daterangeCreateDate[1] }}
                </span>
            </el-col>
          </el-row>
        </div>
        <div class="el-table el-table--enable-row-hover el-table--medium">
          <table cellspacing="0" style="width: 100%;">
            <thead>
            <tr>
              <th class="el-table__cell el-table__cell is-leaf">
                <div class="cell">总单数</div>
              </th>
              <th class="el-table__cell is-leaf">
                <div class="cell">订单总金额</div>
              </th>
              <th class="el-table__cell is-leaf">
                <div class="cell">支付单数</div>
              </th>
              <th class="el-table__cell is-leaf">
                <div class="cell">实付总金额</div>
              </th>
            </tr>
            </thead>
            <tbody v-if="score">
            <tr>
              <td class="el-table__cell is-leaf">
                <div class="cell">{{ score.totalOder }}</div>
              </td>
              <td class="el-table__cell is-leaf">
                <div class="cell">{{ (score.totalPrice / 100).toFixed(2) }}</div>
              </td>
              <td class="el-table__cell is-leaf">
                <div class="cell">{{ score.totalPayOder }}</div>
              </td>
              <td class="el-table__cell is-leaf">
                <div class="cell">{{ (score.payTotalPrice / 100).toFixed(2) }}</div>
              </td>
            </tr>
            </tbody>
          </table>
        </div>
      </el-card>
    </el-row>

    <el-row>
      <el-col :span="24" class="card-box">
        <el-dropdown :hide-on-click="false" style="float: right; padding: 3px 0">
          <span class="el-dropdown-link">
            <el-button icon="el-icon-setting" circle></el-button>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="field in columns" :key="field.key">
              <el-checkbox :value="field.visible" @change="downChange(field.key)">{{field.label}}</el-checkbox>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <!--      订单    -->
        <el-table :data="ordersList" @selection-change="handleSelectionChange">
          <el-table-column label="年级" align="center" key="grade" prop="grade" v-if="columns[0].visible"/>
          <el-table-column label="学校" align="center" key="school" prop="school" v-if="columns[1].visible"/>
          <el-table-column label="学院" align="center" key="college" prop="college" v-if="columns[2].visible"/>
          <el-table-column label="专业" align="center" key="major" prop="major" v-if="columns[3].visible"/>
          <el-table-column label="班级" align="center" key="clazz" prop="clazz" v-if="columns[4].visible"/>
          <el-table-column label="订单号" align="center" key="id" prop="id" v-if="columns[5].visible"/>
          <el-table-column label="订单" align="center" key="orderName" prop="orderName" v-if="columns[6].visible"/>
          <el-table-column label="描述" align="center" key="orderDescrip" prop="orderDescrip" v-if="columns[7].visible"/>
          <el-table-column label="总价" align="center" key="totalPrice" prop="totalPrice" v-if="columns[8].visible">
            <template slot-scope="scope">
              {{ (scope.row.totalPrice / 100).toFixed(2) }}
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" key="status" prop="status" v-if="columns[9].visible">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.trade_state" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="学生" align="center" key="studentName" prop="student.studentName"
                           v-if="columns[11].visible"/>
          <el-table-column label="学号" align="center" key="stuNo" prop="student.stuNo" v-if="columns[12].visible"/>
          <el-table-column label="创建时间" align="center" key="createTime" prop="createTime" width="100"
                           v-if="columns[10].visible"/>
        </el-table>
        <!-- 分页组件-->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {deptSchoolTreeSelect} from "@/api/campus/Org";
import {reportFinanceOrder} from "@/api/report/act_stu";
import {parseTime} from "@/utils/ruoyi";

export default {
  name: "finance",
  components: {Treeselect},
  dicts: ['trade_state'],
  data() {
    return {
      selectedFields: [], // 选中的字段
      // 查询参数
      queryParams: {
        semesterYear: null,
        semester: null,
        deptId: null,
        sysOrgCode: null,
        code: null,
        school: null,
        college: null,
        major: null,
        clazz: null,
        grade: null,
        collegeId: null,
        schoolId: null,
        majorId: null,
        clazzId: null,
        totalPrice: null,
        status: null,
        userId: null,
        studentId: null,
        orderType: null,
        orderName: null,
        orderDescrip: null,
        orderDetail: null,
        outBusinessId: null,
        other1: null,
        other2: null,
        other3: null
      },
      // 时间范围
      daterangeCreateDate: [],
      dateTime: "",
      // 部门树选项
      deptOptions: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 列信息
      columns: [
        {key: 0, label: `年级`, visible: true},
        {key: 1, label: `学校`, visible: false},
        {key: 2, label: `学院`, visible: false},
        {key: 3, label: `专业`, visible: false},
        {key: 4, label: `班级`, visible: true},
        {key: 5, label: `订单号`, visible: false},
        {key: 6, label: `订单`, visible: false},
        {key: 7, label: `描述`, visible: false},
        {key: 8, label: `总价`, visible: true},
        {key: 9, label: `状态`, visible: true},
        {key: 10, label: `创建时间`, visible: false},
        {key: 11, label: `学生`, visible: true},
        {key: 12, label: `学号`, visible: true},
      ],
      // 订单信息
      ordersList: [],
      // 总条数
      total: 0,
      score: {}

    };
  },
  created() {
    // this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询服务器信息 */
    getList() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      this.queryParams.params = {};
      if (null != this.daterangeCreateDate && '' !== this.daterangeCreateDate) {
        this.queryParams.params["beginCreateDate"] = this.daterangeCreateDate[0];
        this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
      }
      reportFinanceOrder(this.queryParams).then(response => {
        this.ordersList = response.data.ordersList;
        this.score = response.data;
        loading.close();
      });

    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在加载服务监控数据，请稍候！");
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.params = {};
      if (null != this.daterangeCreateDate && '' !== this.daterangeCreateDate) {
        this.queryParams.params["beginCreateDate"] = this.daterangeCreateDate[0];
        this.queryParams.params["endCreateDate"] = this.daterangeCreateDate[1];
      }
      const timeName = parseTime(new Date())
      this.download('report/finance/export', {
        ...this.queryParams
      }, `报表数据_${timeName}.xlsx`)
    },
    /** ===============================================部门节点================================================ */
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptSchoolTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    filterTag(value, row) {
      return row.orders.status === value;
    },
    /**
     * 选择一个订单
     * @param row
     */
    handleSelectionChange(row) {

    },
    downChange(val) {
        console.log("key", val)
        const column = this.columns.find(col => col.key === val);
        console.log("col", column)
        if (column) {
          column.visible = !column.visible;
        }
    }
  }
};
</script>
<style>
@media print {
  .do-not-print {
    display: none;
  }
}

.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
</style>
